package cn.acewill.yxd.yunxundian.pubClass;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.model.Employee;
import cn.acewill.yxd.yunxundian.ui.MainActivity;


public class LoginActivity extends BaseActivity {

    private EditText userNum,userPass,IpAddress;
    private Button loginIn;
    public static String Url,uNum,uPass;
    private SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        userLogin();
    }
    public void initView(){
        this.userNum= (EditText) findViewById(R.id.userNum);
        this.userPass= (EditText) findViewById(R.id.userPass);
        this.loginIn= (Button) findViewById(R.id.loginIn);
        this.loginIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userLogin();
            }
        });
    }

    //网络请求，先加载本地数据，无则输入
    protected void userLogin(){

        Url=IpAddress.getText().toString().trim();
        uNum=userNum.getText().toString().trim();
        uPass=userNum.getText().toString().trim();
        if(TextUtils.isEmpty(uNum)||TextUtils.isEmpty(uPass)){
            Toast.makeText(this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(!TextUtils.isEmpty(Url)&&Url.equals("erro")) {
            //登录请求，成功则保存数据saveUser,然后进行页面跳转
            Map<String, Object> map = new HashMap<>();
            map.put("task", "userLogin");
            map.put("userNum", uNum);
            map.put("userPass", uPass);
            //

            //pubClass.Post(this,Url+"/appservice/appservicehzm.aspx", map, new pubClass.MyCallBack<String>() {

            pubClass.Post(this,"http://yxd.acewill.cn/dev/appservice/appservicehzm.aspx", map, new pubClass.MyCallBack<String>() {

                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    pubClass.dialog.dismiss();
                    String[] resArry = result.split("\\|");
                    if (resArry[0].equals("201")) {
                        showAnimaDialog(LoginActivity.this,resArry[2]);
                    } else {
                        showAnimaDialog(LoginActivity.this,"登录成功");
                        Gson gson = new Gson();
                        Employee emps = gson.fromJson(resArry[3], Employee.class);
                        int id = emps.getID();
                        Intent in = new Intent(LoginActivity.this, MainActivity.class);
                        in.putExtra("id", id);
                        startActivity(in);

                    }

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    super.onError(ex, isOnCallback);

                }
            });


            saveUser(Url);
        }
        else{
            Toast.makeText(this,"请输入请求IP",Toast.LENGTH_SHORT).show();
        }


    }


    //IP保存在sharepreference中
    public void saveUser(String URL){
        sp=getSharedPreferences("yxdUrl",MODE_PRIVATE);
        SharedPreferences.Editor ed=sp.edit();
        ed.putString("url",URL);
        ed.commit();
    }


}
